package com.eco.fishway.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author: xulinyu
 * Created at: 2024/8/9 15:04
 */
@Data
@Configuration
public class ThreadPoolConfig {

    @Value("${thread.pool.coreSize:}")
    private int coreSize;

    @Value("${thread.pool.maxSize:}")
    private int maxSize;

    @Value("${thread.pool.keepalive:}")
    private int keepalive;

    @Value("${thread.pool.blockQueueSize:}")
    private int blockQueueSize;

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(){

        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                coreSize,
                maxSize,
                keepalive,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(blockQueueSize),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
        executor.prestartCoreThread();
        return executor;
    }


}
